<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2016/11/9
 * Time: 上午11:12
 */
class ActivityController extends AdminBaseController {

    public function index(){
        $this->setTitle('活动参与度');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $dictType = ActivityService::dictActivityType();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-31 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('act_type', 'select', '活动类型', array('options' => $dictType, 'value' => key($dictTaskType)))
            ->addExport();

        $params = $filter->get();

        $header = ActivityService::indexHeader($params);
        $data   = ActivityService::indexData($params);


        if($filter->isExport()){
            $filename = LibExcel::getFilename('活动参与度', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else {
            $grid->addGrid($header, $data, array(
                'order' => array('GetNumber', 'desc')
            ));

            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
        }
    }

    public function out(){
        $this->setTitle('活动道具/礼包产出');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $dictType = ActivityService::dictActivityType();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-31 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('act_type', 'select', '活动类型', array('options' => $dictType, 'value' => key($dictTaskType)))
            ->addFilter('act_id', 'input', '活动ID');

        $params = $filter->get();

        $itemHeader = ActivityService::itemHeader($params);
        $itemData   = ActivityService::itemData($params);
        $grid->addGrid($itemHeader, $itemData, array(
            'name'  => '道具产出',
            'order' => array('GetNumber', 'desc')
        ));

        $giftHeader = ActivityService::giftHeader($params);
        $giftData   = ActivityService::giftData($params);
        $grid->addGrid($giftHeader, $giftData, array(
            'name'  => '礼包产出',
            'order' => array('GetNumber', 'desc')
        ));

        $chart->setShowType("float");
        $grid->setShowType("float");

        $this->out['filter'] = $filter->show();
        $this->out['chart']  = $chart->show();
        $this->out['grid']   = $grid->show();
    }

    public function finished(){
        $this->setTitle('日常活动完成度');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $dictType = ActivityService::dictActivityType();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-31 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('act_type', 'select', '活动类型', array('options' => $dictType, 'value' => key($dictTaskType)))
            ->addExport();

        $params = $filter->get();

        $header = ActivityService::finishedHeader($params);
        $data   = ActivityService::finishedData($params);


        if($filter->isExport()){
            $filename = LibExcel::getFilename('日常活动完成度', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else {
            $grid->addGrid($header, $data, array(
                'order' => array('GetNumber', 'desc')
            ));

            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
        }
    }

}